package com.honghe.system.service.impl;

import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.honghe.common.utils.SecurityUtils;
import com.honghe.system.domain.SysAttendanceSettings;
import com.honghe.system.mapper.SysAttendanceSettingsMapper;
import com.honghe.system.service.ISysAttendanceSettingsService;

/**
 * 考勤设置Service业务层处理
 * 
 * @author honghe
 */
@Service
public class SysAttendanceSettingsServiceImpl implements ISysAttendanceSettingsService 
{
    @Autowired
    private SysAttendanceSettingsMapper sysAttendanceSettingsMapper;

    /**
     * 查询考勤设置
     * 
     * @return 考勤设置
     */
    @Override
    public SysAttendanceSettings selectSysAttendanceSettings()
    {
        SysAttendanceSettings settings = sysAttendanceSettingsMapper.selectSysAttendanceSettings();
        if (settings == null) {
            // 如果没有设置，插入默认设置
            settings = new SysAttendanceSettings();
            settings.setClockInTime("09:00:00");
            settings.setClockOutTime("18:00:00");
            settings.setUpdateBy(SecurityUtils.getUsername());
            settings.setUpdateTime(new Date());
            sysAttendanceSettingsMapper.insertSysAttendanceSettingsIfNotExists(settings);
            // 重新查询
            settings = sysAttendanceSettingsMapper.selectSysAttendanceSettings();
        }
        return settings;
    }

    /**
     * 更新考勤设置
     * 
     * @param sysAttendanceSettings 考勤设置
     * @return 结果
     */
    @Override
    public int updateSysAttendanceSettings(SysAttendanceSettings sysAttendanceSettings)
    {
        sysAttendanceSettings.setUpdateBy(SecurityUtils.getUsername());
        sysAttendanceSettings.setUpdateTime(new Date());
        
        // 确保设置记录存在
        SysAttendanceSettings existSettings = selectSysAttendanceSettings();
        sysAttendanceSettings.setId(existSettings.getId());
        
        return sysAttendanceSettingsMapper.updateSysAttendanceSettings(sysAttendanceSettings);
    }
} 